package com.example.umbrella.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.umbrella.R;
import com.example.umbrella.model.BluetoothDeviceModel;
import com.google.android.material.card.MaterialCardView;

import java.util.ArrayList;
import java.util.List;

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder> {
    private List<BluetoothDeviceModel> devices = new ArrayList<>();
    private OnDeviceClickListener listener;

    public interface OnDeviceClickListener {
        void onDeviceClick(BluetoothDeviceModel device);
    }

    public void setOnDeviceClickListener(OnDeviceClickListener listener) {
        this.listener = listener;
    }

    public void updateDevices(List<BluetoothDeviceModel> newDevices) {
        devices.clear();
        devices.addAll(newDevices);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.device_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BluetoothDeviceModel device = devices.get(position);

        // 设置设备名称
        holder.deviceName.setText(device.getDeviceName() != null ?
                device.getDeviceName() : "未知设备");

        // 设置设备地址
        holder.deviceAddress.setText(device.getDeviceAddress());

        // 设置Service UUID
        if (device.getServiceUuid() != null) {
            holder.serviceUuid.setText(device.getServiceUuid());
            holder.serviceUuid.setVisibility(View.VISIBLE);
        } else {
            holder.serviceUuid.setVisibility(View.GONE);
        }

        // 设置信号强度指示器
        int rssi = device.getRssi();
        holder.deviceRssi.setText(String.valueOf(rssi));

        MaterialCardView signalIndicator = holder.signalIndicator;
        if (rssi > -65) {
            signalIndicator.setCardBackgroundColor(
                    holder.itemView.getContext().getColor(R.color.signal_strong));
        } else if (rssi > -85) {
            signalIndicator.setCardBackgroundColor(
                    holder.itemView.getContext().getColor(R.color.signal_medium));
        } else {
            signalIndicator.setCardBackgroundColor(
                    holder.itemView.getContext().getColor(R.color.signal_weak));
        }

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDeviceClick(device);
            }
        });
    }

    @Override
    public int getItemCount() {
        return devices.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView deviceName;
        TextView deviceAddress;
        TextView serviceUuid;
        TextView deviceRssi;
        MaterialCardView signalIndicator;

        ViewHolder(View view) {
            super(view);
            deviceName = view.findViewById(R.id.device_name);
            deviceAddress = view.findViewById(R.id.device_address);
            serviceUuid = view.findViewById(R.id.service_uuid);
            deviceRssi = view.findViewById(R.id.device_rssi);
            signalIndicator = view.findViewById(R.id.signal_indicator);
        }
    }

    // 添加单个设备更新方法
    public void updateDevice(BluetoothDeviceModel device) {
        for (int i = 0; i < devices.size(); i++) {
            if (devices.get(i).getDeviceAddress().equals(device.getDeviceAddress())) {
                devices.set(i, device);
                notifyItemChanged(i);
                return;
            }
        }
        devices.add(device);
        notifyItemInserted(devices.size() - 1);
    }

    // 清空设备列表
    public void clearDevices() {
        devices.clear();
        notifyDataSetChanged();
    }

    // 获取当前设备列表
    public List<BluetoothDeviceModel> getDevices() {
        return new ArrayList<>(devices);
    }

    // 移除单个设备
    public void removeDevice(String deviceAddress) {
        for (int i = 0; i < devices.size(); i++) {
            if (devices.get(i).getDeviceAddress().equals(deviceAddress)) {
                devices.remove(i);
                notifyItemRemoved(i);
                return;
            }
        }
    }
}